#include "kernel_operator.h"
using namespace AscendC;
constexpr int32_t BUFFER_NUM = 2;
class KernelFastGelu
{
public:
  __aicore__ inline KernelFastGelu() {}
  __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, uint32_t blockLength,
                              uint32_t tileNum, uint32_t tileLength,
                              uint32_t lasttileLength, uint32_t typeKey)
  {
    ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");
    this->typeKey = typeKey;
    this->blockLength = blockLength;
    this->tileNum =
        tileNum ASSERT(tileNum != 0 && "tile num can not be zero!");
    this->tileLength = tileLength / BUFFER_NUM;
    this->lasttileLength = lasttileLength;
    xGm.SetGlobalBuffer((__gm__ DTYPE_X *)x,
                        this->blockLength);
    yGm.SetGlobalBuffer((__gm__ DTYPE_X *)y,
                          this->blockLength);
    pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(DTYPE_X));
    pipe.InitBuffer(outQueueY, BUFFER_NUM, this->tileLength * sizeof(DTYPE_X));
    pipe.InitBuffer(calcBuf, this->tileLength * sizeof(DTYPE_X));
  }
  __aicore__ inline void Process()
  {
    int32_t loopCount = this->tileNum * BUFFER_NUM;
    for (int32_t i = 0; i < loopCount; i++)
    {
      if(BUFFER_NUM == 2 && i == loopCount - 2 && lasttileLength <= tileLength){
        continue;
      }
      CopyIn(i);
      Compute(i);
      CopyOut(i);
    }
  }

private:
  __aicore__ inline void CopyIn(int32_t progress)
  {
    LocalTensor<DTYPE_X> xLocal = inQueueX.AllocTensor<DTYPE_X>();

    if (BUFFER_NUM == 2) {
      if ((progress == (this->tileNum * BUFFER_NUM - 2)) ||
          (progress == (this->tileNum * BUFFER_NUM - 1))) {
        DataCopy(
            xLocal[0],
            xGm[(progress - 2) * (this->tileLength) + this->lasttileLength],
            (this->tileLength));
      }
      else {
        DataCopy(xLocal[0], xGm[progress * (this->tileLength)],
                 (this->tileLength));
      }
    }
    inQueueX.EnQue(xLocal);
    
  }
  __aicore__ inline void Compute(int32_t progress)
  {
    LocalTensor<DTYPE_X> xLocal = inQueueX.DeQue<DTYPE_X>();
    LocalTensor<DTYPE_X> yLocal = outQueueY.AllocTensor<DTYPE_X>();
    LocalTensor<DTYPE_X> tmpLocal = calcBuf.Get<DTYPE_X>();
    // compute
    // Abs(tmpLocal, xLocal, this->tileLength);
    
    // Sub(yLocal, xLocal, tmpLocal, this->tileLength);
    // Muls(yLocal, yLocal, (DTYPE_X)0.851, this->tileLength);
    // Exp(yLocal, yLocal, this->tileLength);
    // Mul(yLocal, xLocal, yLocal, this->tileLength); // div_up compute end

    // Muls(xLocal, tmpLocal, (DTYPE_X)-1.702, this->tileLength);
    // Exp(xLocal, xLocal, this->tileLength);
    // Adds(xLocal, xLocal, (DTYPE_X)1.0, this->tileLength); // div_down compute end

    // Div(yLocal, yLocal, xLocal, this->tileLength);
    
    Muls(tmpLocal, xLocal, (DTYPE_X)-1.702, this->tileLength);// -1.702x
    Exp(tmpLocal, tmpLocal, this->tileLength); // e^(-1.702x)
    Adds(tmpLocal, tmpLocal, (DTYPE_X)1.0, this->tileLength); // 1+e^(-1.702x)
    Div(yLocal, xLocal, tmpLocal, this->tileLength); // x / 1+e^(-1.702x)

    outQueueY.EnQue<DTYPE_X>(yLocal);
    inQueueX.FreeTensor(xLocal);
  }
  __aicore__ inline void CopyOut(int32_t progress)
  {
    LocalTensor<DTYPE_X> yLocal = outQueueY.DeQue<DTYPE_X>();

    if (BUFFER_NUM == 2) {
      if ((progress == (this->tileNum * BUFFER_NUM - 2)) ||
          (progress == (this->tileNum * BUFFER_NUM - 1))) {
        DataCopy(
            yGm[(progress - 2) * (this->tileLength) + this->lasttileLength],
            yLocal, (this->tileLength));
      }
      else {
        DataCopy(yGm[progress * (this->tileLength)], yLocal,
                 (this->tileLength));
      }
    }

    outQueueY.FreeTensor(yLocal);
  }

private:
  TPipe pipe;
  TQue<QuePosition::VECIN, BUFFER_NUM> inQueueX;
  TQue<QuePosition::VECOUT, BUFFER_NUM> outQueueY;
  TBuf<QuePosition::VECCALC> calcBuf;
  GlobalTensor<DTYPE_X> xGm;
  GlobalTensor<DTYPE_Y> yGm;
  uint32_t blockLength;
  uint32_t tileNum;
  uint32_t tileLength;
  uint32_t lasttileLength;
  uint32_t typeKey;
};
extern "C" __global__ __aicore__ void fast_gelu(GM_ADDR x, GM_ADDR y, GM_ADDR workspace, GM_ADDR tiling) {
    GET_TILING_DATA(tiling_data, tiling);
    KernelFastGelu op;

    op.Init(x, y, tiling_data.blockLength,
            tiling_data.tileNum, tiling_data.tileLength,
            tiling_data.lasttileLength, tiling_data.typeKey);
    op.Process();
    // TODO: user kernel impl
}
#ifndef __CCE_KT_TEST__
void fast_gelu_do(uint32_t blockDim, void* l2ctrl, void* stream,
                       uint8_t* x, uint8_t* y,
                       uint8_t* workspace, uint8_t* tiling) {
  fast_gelu<<<blockDim, l2ctrl, stream>>>(x, y, workspace, tiling);
}
#endif